package jsyf.oa.service.intentCustomerReportNew;


import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.intentCustomerReportNew.IntentCustomerReportNewDao;
import jsyf.oa.facade.intentCustomerReportNew.IntentCustomerReportNewFacade;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Created by 92852 on 2018/9/4.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class IntentCustomerReportNewService implements IntentCustomerReportNewFacade {
    @Autowired
    private IntentCustomerReportNewDao intentCustomerReportNewDao;
    @Autowired
    private ReportCommonMethodsAndConstant common;


    @Override
    public String save(String jsonData) {
        return null;
    }

    /*集团*/
    @Override
    public List<JSONObject> getForGroupIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = common.getAllCompanysIdByUserId(userId);
        return intentCustomerReportNewDao.getForGroupIntentCustomerList(companyId);
    }

    @Override
    public JSONObject getAllReportCustomer(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");
        StringBuffer condition = new StringBuffer();
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND (t_wrt_customer.`NAME` LIKE '%"+key+"%' OR  t_wrt_customer.`TEL` LIKE '%"+key+"%'  )");
        }
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        /*获取身份标志*/
        JSONObject roleFlag = intentCustomerReportNewDao.getManageFlagAndFrontFlagAndAreaTypeByUserId(userId);
        /*获取集团人员可以看到的公司*/
        String companyId = common.getAllCompanysIdByUserId(userId);
        /*获取所有意向客户*/
        List<JSONObject> records = intentCustomerReportNewDao.getAllIntentCustomerList(userId,condition.toString(),companyId, requestPage, pageSize, roleFlag);
        /*获得查询数目*/
        int recordCount = intentCustomerReportNewDao.getCountByCompanyIdAndUserIdAndKey(companyId, userId, condition.toString(), roleFlag);

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    /*集团页面点击公司*/
    @Override
    public List<JSONObject> getForDepartByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        return intentCustomerReportNewDao.getForDepartByCompanyId(companyId);
    }

    /*获取个人*/
    @Override
    public JSONObject getForPersonalIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String key = jsonParams.getString("key");

        StringBuffer condition = new StringBuffer();
        if (StringUtils.isNotBlank(key) && StringUtils.isNotEmpty(key)) {
            condition.append("AND( t_wrt_customer.`NAME` LIKE " + " '%" + key + "%'" +
                    " OR(t_wrt_customer.INTENTION_MODEL LIKE " + " '%" + key + "%'" + ") " +
                    " OR(t_wrt_customer.BUY_DATE LIKE " + " '%" + key + "%'" + ") " +
                    " OR(t_wrt_customer.BUY_TYPE LIKE " + " '%" + key + "%'" + ") " +
                    " OR(t_wrt_customer.OTHER_CONDITION LIKE " + " '%" + key + "%'" + ") " +
                    " OR(t_wrt_customer.TEL LIKE " + " '%" + key + "%'" + ")  )" + "\n");
        }
        /*分页信息*/
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        List<JSONObject> records = intentCustomerReportNewDao.getForPersonalIntentCustomerList(userId, condition.toString(), requestPage, pageSize);
        int recordCount = intentCustomerReportNewDao.getCountPersonalIntentCustomerList(userId, condition.toString());
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1;
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }


    @Override
    public List<JSONObject> getForCompanyIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotEmpty(userId)) {
              /*获取人员可以看到的公司*/
            String companyId = common.getAllCompanysIdByUserId(userId);
            return intentCustomerReportNewDao.getForCompanyIntentCustomerList(companyId);
        }else {
            return null;
        }

    }

    @Override
    public List<JSONObject> getForDepartIntentCustomerList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        return intentCustomerReportNewDao.getForDepartIntentCustomerList(userId);
    }










}
